#################################################################
## CAN FOREIGN AID REDUCE THE DESIRE TO EMIGRATE? ###############
## EVIDENCE FROM A RANDOMIZED CONTROLLED TRIAL ##################
## Simon, Schwartz, and Hudson ##################################
## April 2024 ###################################################
## Replication Script ###########################################
## Table 1 ######################################################
#################################################################

##### Packages
library(tidyverse)
library(knitr)
library(kableExtra)
options(knitr.table.format = "latex")
options(digits=4)
library(scales)


##### WIDE DATASET 
mig.wide <- read.csv("mig.wide.csv")


# weighted table

business.tab <- 
  mig.wide %>%
  group_by(treat.dum) %>%
  drop_na(wt3) %>%
  summarise(location = (weighted.mean(bus.location, wt3, na.rm=T))*100,
            open = (weighted.mean(bus.open, wt3, na.rm=T))*100,
            purchases = (weighted.mean(bus.purchases, wt3, na.rm=T))*100,
            hire = (weighted.mean(bus.hire, wt3, na.rm=T))*100,
            profit = (weighted.mean(bus.profit, wt3, na.rm=T))*100,
            close = (weighted.mean(bus.close, wt3, na.rm=T))*100)

# View table
business.tab  

# Create table in Latex
kbl(business.tab, booktabs = T, "latex", escape = F,
    col.names = linebreak(c("Treatment\nGroup", "Selected\nLocation", "Opened\nBusiness", "Made\nPurchases",
                            "Hired\nEmployees", "Made\nProfit", "Closed\nBusiness"), align="c"),
    caption = "Percent achieving key business outcomes") %>%
  column_spec(1, border_right = T) %>%
  column_spec(2, bold = T) %>%
  column_spec(3, bold = T) %>%
  column_spec(4, bold = T) %>%
  column_spec(5, bold = T) %>%
  column_spec(6, bold = T) %>%
  footnote(general = "Percentages shown in bold reflect group differences that are significant at <0.05")
